// pages/malltaskPackage/pages/task/task.js
const app = getApp();
const quote = app.globalData.quote;
const path = quote.paths;
const util = require('../../../../utils/util.js');
const tools = require('../../../../utils/tools.js');
const api = require('../../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskList: [],
    growList: [],
    noviceList: [],
    dayList: [],
    IsReceive:null,
    novice: null,
    IsAuthorization: null,
    nummber: 0,
    show: false,
    hide: true,
    Gold:0,
    IsVip:null,
    Rank:1,
    Experience:0,
    Gold:null,
    UpgradeExperience:100,
    progress:null,  
    view:null, 
    shoaw:false, 
    progress_2:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      IsAuthorization: app.globalData.IsAuthorization,
      IsVip: app.globalData.IsVip,
      Gold: app.globalData.Gold,
    })
    that.getuser()
    that.taskList()
  },
  //任务列表
  taskList:function(){
    var that = this 
    var growList = []
    var noviceList = []
    var dayList = []
    wx.showLoading({
      title: '加载中',
    })
    let dataDic = {
      UCUID: app.globalData.UCUID
    };
    api.methodEnglishCorner_Task({
      dataDic,
      success: function (res) {
        wx.hideLoading()
        console.log(res.data.data.TaskList, '任务列表')
        var taskList = res.data.data.TaskList;
        taskList.forEach(item => {
          console.log(item,'**********************')
          if (item.TaskType == 2) {
            var progress_2 = (that.data.Rank/ item.TaskExperience) * 100
            growList.push(item)
            //console.log(growList,'33')
            that.setData({
              growList: growList,
              progress_2: progress_2
            })
            console.log(that.data.growList, '44成就任务列表')
          } else if (item.TaskType == 1 || item.TaskType  == 3) {
            noviceList.push(item)
            that.setData({
              noviceList: noviceList,
            })
            if (that.data.noviceList[0].TaskType == 1) {
              that.setData({
                novice: 1,
              })
            } else if (that.data.noviceList[0].TaskType == 3) {
              that.setData({
                novice: 0,
              })
            }
            console.log(that.data.noviceList, '53新手任务列表')

          } else if (item.TaskType == 1) {
            noviceList.push(item)
            var progress =  (item.Duration / item.TaskExperience)*100
            that.setData({
              noviceList: noviceList,
              progress: progress

            })
            console.log(that.data.noviceList, '60每日任务列表')
          }

        })
        // that.setData({
        //   taskList: res.data.data.TaskList
        // })
      }
    })
  },
  //获取用户金币等级
  getuser:function(){
    var that  = this 
   let dataDic = {
     UCUID: app.globalData.UCUID
   };
    api.methodEnglishCorner_MyPersonalInformation({
      dataDic,
      success:function(res){
        var bar = (res.data.data.Experience / res.data.data.UpgradeExperience )*100
        console.log(bar)
        console.log(res.data,'91用户资料')
        that.setData({
          bar: bar,
          Experience: res.data.data.Experience,
          Gold: res.data.data.Gold,
          Rank: res.data.data.Rank,
          UpgradeExperience: res.data.data.UpgradeExperience
        })
      }
    })
  },
  //查看说明
  view:function(){
    var that = this 
    that.setData({
      view:true,
      shoaw:true
    })
  },
  //隐藏弹窗
  clone:function(){
    var that = this 
    that.setData({
      view:false,
      shoaw: false
    })
  },
  //进入商城
  goshop: function() {
    var that = this
    console.log('11')
    wx.navigateTo({
      url: '../../../malltaskPackage/pages/mall/mall'
    })
  },
  //领取登录任务
  receive:function(e){
   var that  = this 
   console.log(e)
    var receiveid = e.currentTarget.dataset.receive
   let dataDic = {
     TaskId: receiveid,
     UCUID: app.globalData.UCUID
   };
    api.methodEnglishCorner_ReceiveTask({
      dataDic,
      success:function(res){
        console.log(res,'105领取任成功')
        that.taskList()
        that.getuser()
        if(res.data.code == 0){
          wx.showToast({
            icon: 'success',
            title: '领取成功',
            duration: 2000,
          })
         

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})